package mainpackage;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

public class Todo {
	// todo's data...
	private int id;
	private String name;
	private String description;
	private Date startTime;
	private Date endTime;
	private Category category;

	private static int lastId = 0;
	
	public static void setLastId(int id) {
		lastId = id;
	}

	public Todo(String name, String description, Date startTime, Date endTime,
			Category category) throws EndDateIsBeforeStartException {
		fillData(name, description, startTime, endTime, category);
	}

	/**
	 * more user-friendly constructor (dates and category as strings)
	 * 
	 * @param name
	 * @param description
	 * @param startTime
	 *            yy/MM/dd H:m
	 * @param endTime
	 *            yy/MM/dd H:m
	 * @param category
	 * @throws EndDateIsBeforeStartException
	 * @throws ParseException wrong date format
	 */
	public Todo(String name, String description, String startTime,
			String endTime, Category category)
			throws EndDateIsBeforeStartException, ParseException {
		Date convertedStart = stringToDate(startTime);
		Date convertedEnd = stringToDate(endTime);
		fillData(name, description, convertedStart, convertedEnd, category);
	}

	/**
	 * @param time
	 *            yy/MM/dd H:m
	 * @return
	 * @throws ParseException
	 *             date is in wrong format (must be yy/MM/dd H:m)
	 */
	private Date stringToDate(String time) throws ParseException {
		SimpleDateFormat dateFormat = new SimpleDateFormat("yy/MM/dd H:m");
		Date converted = null;
		converted = dateFormat.parse(time);
		return converted;
	}

	private void fillData(String name, String description, Date startTime,
			Date endTime, Category category)
			throws EndDateIsBeforeStartException {
		if (!startTime.before(endTime)) {
			throw new EndDateIsBeforeStartException();
		}
		this.name = name;
		this.description = description;
		this.startTime = startTime;
		this.endTime = endTime;
		this.category = category;
		this.id = ++lastId;
	}

	/**
	 * package-access method for changing data
	 * 
	 * @param name
	 * @param description
	 * @param startTime2
	 * @param endTime2
	 * @param category
	 * @throws EndDateIsBeforeStartException
	 * @throws ParseException wrong date format
	 */
	void editData(String name, String description, String startTime,
			String endTime, Category category)
			throws EndDateIsBeforeStartException, ParseException {
		if (!stringToDate(startTime).before(stringToDate(endTime))) {
			throw new EndDateIsBeforeStartException();
		}
		this.name = name;
		this.description = description;
		this.startTime = stringToDate(startTime);
		this.endTime = stringToDate(endTime);
		this.category = category;
	}

	// auto-generated getters and setters:
	public int getId() {
		return id;
	}

	public String getName() {
		return name;
	}

	public String getDescription() {
		return description;
	}

	public Date getStartTime() {
		return startTime;
	}

	public Date getEndTime() {
		return endTime;
	}

	public Category getCategory() {
		return category;
	}

}
